//import net.sf.json.JSONObject;
//import javax.swing.*;
//import java.awt.*;
//import java.io.DataInputStream;
//import java.io.DataOutputStream;
//import java.io.IOException;
//import java.net.Socket;
//
//
//public class Login extends JFrame{
//
//    public static void main(String[] args) {
//        new Login();
//    }
//
//    public Login(){
//
//        setTitle("登录");
//        setLayout(null);
//        setSize(320,470);
//        setLocationRelativeTo(null);
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        setResizable(false);
//
//        //logo
//        ImageIcon logo = new ImageIcon("media/logo.png");
//        setIconImage(logo.getImage());
//
//        //用户头像面板
//        ImageIcon avatar = new ImageIcon("media/avatar.png");
//        ImagePanel imagePanel = new ImagePanel(avatar.getImage());
//        imagePanel.setBounds(100,5,100,100);
//        add(imagePanel);
//
//        //界面标题
//        JLabel headTitle = new JLabel("登录",SwingConstants.CENTER);
//        headTitle.setFont(new Font(null, Font.BOLD, 30));  // 设置文本的字体类型、样式 和 大小
//        headTitle.setBounds(100,100,100,50);
//        add(headTitle);
//
//        //用户名标题
//        JLabel username_label = new JLabel("昵称:");
//        username_label.setBounds(50,140,100,50);
//        username_label.setFont(new Font(null, Font.PLAIN, 18));
//        add(username_label);
//
//        //输入用户名的文本区域
//        JTextField username_field = new JTextField();
//        username_field.setBounds(50,180,200,35);
//        username_field.setFont(new Font(null, Font.PLAIN, 13));
//        add(username_field);
//
//        //登录按钮
//        JButton login = new JButton("登录");
//        login.setBounds(100,290,100,40);
//        login.setFont(new Font(null,Font.PLAIN,15));
//        login.setBackground(new Color(0,191,255));
//        add(login);
//
//        setVisible(true);   //设置是否可见
//
//        //登录按钮点击事件
//        login.addActionListener(e -> {
//            String username = username_field.getText();
//
//            JSONObject data = new JSONObject();
//            data.put("username", username);
//            data.put("link_type", "attempt_login");
//
//            try {
//                Socket socket = new Socket(Utils.ServerIP, Utils.SeverThreadPort);
//                DataInputStream inputStream = new DataInputStream(socket.getInputStream());
//                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
//                outputStream.writeUTF(data.toString());
//
//                String confirm = inputStream.readUTF();
////                System.out.println(confirm);
//                if(confirm.equals("accepted")){
////                    System.out.println("昵称有效");
//                    setVisible(false);
//                    new Client(username);
//                }
//                else {
//                    JOptionPane.showMessageDialog(null,"昵称重复","提示",
//                            JOptionPane.WARNING_MESSAGE);
//                }
//                socket.close();
//
//            } catch (IOException ecp) {
//                JOptionPane.showMessageDialog(null,"服务器无响应","提示",
//                        JOptionPane.WARNING_MESSAGE);
//            }
//
//
//        });
//    }
//
//}
//
//
//
import net.sf.json.JSONObject;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class Login extends JFrame{

    public static void main(String[] args) {
        // 设置现代化的外观
        try {
            UIManager.setLookAndFeel(new com.formdev.flatlaf.FlatIntelliJLaf());
        } catch (Exception ex) {
            System.err.println("无法设置外观: " + ex.getMessage());
        }

        new Login();
    }

    public Login(){

        setTitle("QQIQ");
        setSize(320,470);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        // 设置窗口图标
        ImageIcon logo = new ImageIcon("media/RC.png");
        setIconImage(logo.getImage());

        // 使用面板和布局管理器
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BorderLayout(0, 0));
        add(mainPanel);

        // 顶部面板，包含头像和标题
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
        topPanel.setBackground(new Color(255, 255, 255, 178));
        mainPanel.add(topPanel, BorderLayout.NORTH);

        // 用户头像
        ImageIcon originalIcon = new ImageIcon("media/RC.png");
        Image image = originalIcon.getImage().getScaledInstance(200, 200, Image.SCALE_SMOOTH);
        ImageIcon avatarIcon = new ImageIcon(image);
        JLabel avatarLabel = new JLabel(avatarIcon);
        avatarLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        topPanel.add(Box.createVerticalStrut(10));
        topPanel.add(avatarLabel);

        // 界面标题
        JLabel headTitle = new JLabel("QQIQ 2024", SwingConstants.CENTER);
        headTitle.setFont(new Font("华文琥珀", Font.PLAIN, 50));
        headTitle.setForeground(new Color(0, 0, 0, 129));
        headTitle.setAlignmentX(Component.CENTER_ALIGNMENT);
        topPanel.add(Box.createVerticalStrut(20));
        topPanel.add(headTitle);

        // 中部面板，包含用户名输入和按钮
        JPanel centerPanel = new JPanel();
//        centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
        centerPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 20));
        centerPanel.setBackground(new Color(255, 255, 255, 179));
        mainPanel.add(centerPanel, BorderLayout.CENTER);

        // 用户名标签
        JLabel username_label = new JLabel("账户");
        username_label.setFont(new Font("微软雅黑", Font.BOLD, 20));
        username_label.setForeground(new Color(0, 0, 0, 129));
        username_label.setAlignmentX(Component.CENTER_ALIGNMENT);
        centerPanel.add(Box.createVerticalStrut(50));
        centerPanel.add(username_label);

        // 用户名输入框
        JTextField username_field = new JTextField();
        username_field.setMaximumSize(new Dimension(200,35));
        username_field.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        username_field.setPreferredSize(new Dimension(150,35));
        username_field.setAlignmentX(Component.CENTER_ALIGNMENT);
        centerPanel.add(Box.createVerticalStrut(50));
        centerPanel.add(username_field);

        // 登录按钮
        // 创建图标标签
        ImageIcon loginIcon = new ImageIcon("media/arrow_right_circle.png"); // 替换为实际的图标路径
        JLabel login = new JLabel(loginIcon);
        login.setAlignmentX(Component.CENTER_ALIGNMENT);
        login.setMaximumSize(new Dimension(100, 40));
        centerPanel.add(Box.createVerticalStrut(50));
        centerPanel.add(login);

        // 设置窗口背景色
        mainPanel.setBackground(Color.BLACK);
        getContentPane().setBackground(Color.BLACK);

        setVisible(true);   // 显示窗口
        // 登录图标点击事件
        login.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // 获取用户名输入框的值
                String username = username_field.getText();

                JSONObject data = new JSONObject();
                data.put("username", username);
                data.put("link_type", "attempt_login");

                try {
                    Socket socket = new Socket(Utils.ServerIP, Utils.SeverThreadPort);
                    DataInputStream inputStream = new DataInputStream(socket.getInputStream());
                    DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
                    outputStream.writeUTF(data.toString());

                    String confirm = inputStream.readUTF();
                    if (confirm.equals("accepted")) {
                        setVisible(false);
                        new Client(username);
                    } else {
                        JOptionPane.showMessageDialog(null, "昵称重复", "提示", JOptionPane.WARNING_MESSAGE);
                    }
                    socket.close();
                } catch (IOException ecp) {
                    JOptionPane.showMessageDialog(null, "服务器无响应", "提示", JOptionPane.WARNING_MESSAGE);
                }
            }
        });
        // 登录按钮点击事件
//        login.addActionListener(e -> {
//            String username = username_field.getText();
//
//            JSONObject data = new JSONObject();
//            data.put("username", username);
//            data.put("link_type", "attempt_login");
//
//            try {
//                Socket socket = new Socket(Utils.ServerIP, Utils.SeverThreadPort);
//                DataInputStream inputStream = new DataInputStream(socket.getInputStream());
//                DataOutputStream outputStream = new DataOutputStream(socket.getOutputStream());
//                outputStream.writeUTF(data.toString());
//
//                String confirm = inputStream.readUTF();
//                if(confirm.equals("accepted")){
//                    setVisible(false);
//                    new Client(username);
//                }
//                else {
//                    JOptionPane.showMessageDialog(null,"昵称重复","提示",
//                            JOptionPane.WARNING_MESSAGE);
//                }
//                socket.close();
//
//            } catch (IOException ecp) {
//                JOptionPane.showMessageDialog(null,"服务器无响应","提示",
//                        JOptionPane.WARNING_MESSAGE);
//            }
//        });
    }
}